package sxe.core.math;

/*
Created on Jun 17, 2005
(C) 2005 
@author Michael Long

This program is free software; you can redistribute it and/or
modify it under the terms of Version 2 of the GNU General Public 
License.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

import snobject.snDouble;
import snobject.snInteger;
import snobject.snObject;
import snobject.snObjectArgList;
import step.FunctionWrapper;
import step.EvaluationFailure;

/**
 * Returns the subtraction of a set of values
 * @param argv must contain snInteger or snDouble objects in the stack
 * @return returns the subtraction of the arguments
 */

public class subtract extends FunctionWrapper{
	
	public snObject Call(snObjectArgList argv) throws EvaluationFailure{
		// REQUIRES: argv must contain snInteger or snDouble objects
		// EFFECTS: returns the snObject of the subtraction of the values from argv
		boolean isDbl = false;
		double ret = 0;
		int index = argv.index();
		
		if(argv.popObject() instanceof snDouble){
			ret = ((snDouble)argv.objectAt(index)).getDouble();
			isDbl = true;
		}
		else
			ret = ((snInteger)argv.objectAt(index)).getInteger();
	
	
		while(argv.hasNext()){
			index++;
			if(argv.popObject() instanceof snDouble){
				ret -= ((snDouble)argv.objectAt(index)).getDouble();
				isDbl = true;
		}
			else
				ret -= ((snInteger)argv.objectAt(index)).getInteger();
		}
			
		if(isDbl)
			return new snDouble(new Double(ret));
		else
			return new snInteger(new Integer((new Double (ret)).intValue()));
	}
}
